package com.ibeeking.found.cop.b.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.cop.b.rest.service.ICopChannelService;
import com.ibeeking.found.cop.service.common.dos.CopChannelDO;
import com.ibeeking.found.cop.service.common.param.CopChannelParam;
import com.ibeeking.found.cop.service.common.query.CopChannelPageQuery;
import com.ibeeking.found.cop.service.common.query.CopChannelQuery;
import com.ibeeking.found.cop.service.common.vo.CopChannelVO;
import com.ibeeking.found.cop.service.mapper.CopChannelMapper;
import com.ibeeking.found.id.GenerateId;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName CopChannelServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class CopChannelServiceImpl  extends ServiceImpl<CopChannelMapper, CopChannelDO> implements ICopChannelService {

    @Resource
    private CopChannelMapper copChannelMapper;

    @Override
    public List<CopChannelVO> list(CopChannelQuery query) {
        List<CopChannelDO> entityDOS = copChannelMapper.selectList(new LambdaQueryWrapper<CopChannelDO>()
                .eq(query.getId() != null, CopChannelDO::getId, query.getId())
                .like(StringUtils.isNotBlank(query.getName()), CopChannelDO::getName, query.getName())
                .eq(query.getStatus() != null, CopChannelDO::getStatus, query.getStatus())
                .orderByDesc(CopChannelDO::getCreateTime)
        );
        List<CopChannelVO> entityVOS =JsonUtils.jsonToList(entityDOS, CopChannelVO.class);
        entityVOS.forEach(x -> {
            x.setStatusDesc(PublishStatusEnum.getName(x.getStatus()));
        });
        return entityVOS;
    }

    @Override
    public Page<CopChannelVO> pageList(CopChannelPageQuery pageQuery) {
        LambdaQueryWrapper<CopChannelDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pageQuery.getId() != null, CopChannelDO::getId, pageQuery.getId())
                .like(StringUtils.isNotBlank(pageQuery.getName()), CopChannelDO::getName, pageQuery.getName())
                .eq(pageQuery.getStatus() != null, CopChannelDO::getStatus, pageQuery.getStatus())
                .orderByDesc(CopChannelDO::getCreateTime);
        Page<CopChannelDO> entityDOPages = copChannelMapper.selectPage(new Page<CopChannelDO>(pageQuery.getPageNum(), pageQuery.getPageSize()), wrapper);
        Page<CopChannelVO> entityVOPages = BeanUtil.convertPage(entityDOPages, CopChannelVO.class);
        entityVOPages.getRecords().forEach(f ->{
            f.setStatusDesc(PublishStatusEnum.getName(f.getStatus()));
        });
        return entityVOPages;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(CopChannelParam param) {
        checkUnique(param);
        CopChannelDO entityDO = BeanUtil.convertBean(param, CopChannelDO.class);
        entityDO.setId(GenerateId.generateId());
        entityDO.setCreateBy(UserUtils.getUserId());
        entityDO.setCreateTime(LocalDateTime.now());
        entityDO.setModifyTime(LocalDateTime.now());
        return copChannelMapper.insert(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modify(CopChannelParam param) {
        CopChannelDO entityDO = BeanUtil.convertBean(param, CopChannelDO.class);
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        return copChannelMapper.updateById(entityDO) > 0;
    }

    @Override
    public CopChannelVO view(Long id) {
        CopChannelDO entityDO = copChannelMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        CopChannelVO entityVO = BeanUtil.convertBean(entityDO, CopChannelVO.class);
        if(entityVO != null) {
            entityVO.setStatusDesc(PublishStatusEnum.getName(entityDO.getStatus()));
        }
        return entityVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeStatus(Long id, Integer status) {
        CopChannelDO entityDO = copChannelMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        entityDO.setStatus(status);
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        return copChannelMapper.updateById(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean del(Long id) {
        CopChannelDO entityDO = copChannelMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        return copChannelMapper.deleteById(id) > 0;
    }
    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(CopChannelParam param) {
        if (param == null) {
            throw new BusinessException("当前记录不存在!");
        }
        //code name确保等级唯一性
        LambdaQueryWrapper<CopChannelDO> queryWrapper = new LambdaQueryWrapper<CopChannelDO>()
                .ne(param.getId() != null, CopChannelDO::getId, param.getId())
                .eq(CopChannelDO::getName, param.getName());
        List<CopChannelDO> dos = copChannelMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在同名的记录!");
        }
    }

}
